const common = {
	navBarBg: {backgroundImage: "linear-gradient(90deg, #12A0EA, #1DC8B5)"},
	formLabelStyle:{
		fontSize: "32rpx"
	},
	buttonStyle: {
		backgroundImage: "linear-gradient(90deg, #12A0EA, #1DC8B5)",
		color: "#FFF",
		fontSize: "32rpx"
	},
	inputStyle:{
		fontSize: "32rpx"
	},
	swiperBarStyle :{
		"bottom":"18rpx",
		"height": "25rpx",
		"border-radius": "20rpx",
		"background-color": "#95DCDE",
		"background-size": "100% 100%"
	},
	rptDetailTagStyle: {
		b:'font-weight:bold;font-size:50rpx;position:relative;top: 6rpx;',
	},
	toast: function(text, duration, success) {
		uni.showToast({
			title: text,
			icon: success ? 'success' : 'none',
			duration: duration || 2000,
			position: "top"
		})
	},
	//显示引导页
	showGuidePage: function(){
		try {
			const value = uni.getStorageSync('EYTRPT_LaunchFlag');
			if (value) {
				if (value == true) 
					return;
			} else {
				uni.redirectTo({
					url: '../platforms/app-plus/eyunt/guide'
				});
			}
		} catch(e) {}
	},
	//获取URL参数
	getQueryString(name) {
		var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
		var r = window.location.search.substr(1).match(reg);
		if (r != null) {
			console.log(decodeURI(r[2]))
			//return unescape(r[2]);
			return r[2];
		}
		return null;
	},
	//获取医院信息
	getHosInfo(hosId) {
		uni.request({
			url: getApp().globalData.ServerURL("/api/getHosInfo"),
			method: "POST",
			data:{
				id: hosId
			},
			success(res) {
				if (res.data.success) {
					console.log(res.data)
					getApp().globalData.HospitalInfo = res.data;
				}
			}
		});
	},
	//检测用户是否已登录
	checkPerson(backURL) {
		if (backURL !== undefined && backURL !== "") {
			uni.setStorageSync("EYTRPT_BackURL", backURL);
		}
		let per = getApp().globalData.PersonInfo;
		//console.log(per)
		if (per===undefined || per===null || per.mobile === undefined) {
			//#ifdef MP-WEIXIN
			uni.reLaunch({
				url: "/pages/login/index",
				animationType: "none"
			});
			//#endif
			//#ifdef H5 || APP-PLUS
			uni.redirectTo({
				url: "/pages/login/index",
				animationType: "none"
			});
			//#endif
			return false;
		}
		return true;
	},
	//验证手机号
	checkPhone(sPhone){ 
		var pattern = /^1[34578]\d{9}$/;
		if (pattern.test(sPhone)) {
			return true;
		}
		return false;
	},
	formatDate(time,format='YY-MM-DD'){
		if (time === undefined)
			var date = new Date();
		else
			var date = new Date(time);
	 
	    var year = date.getFullYear(),
	        month = date.getMonth()+1,//月份是从0开始的
	        day = date.getDate();

	    var preArr = Array.apply(null,Array(10)).map(function(elem, index) {
	        return '0'+index;
	    });//开个长度为10的数组 格式为 ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09"]
	 
	    var newTime = format.replace(/YY/g,year)
	        .replace(/MM/g,preArr[month]||month)
	        .replace(/DD/g,preArr[day]||day)
	 
	    return newTime;
	},
	formatDatetime(time,format='YY-MM-DD hh:mm:ss'){
		if (time === undefined)
			var date = new Date();
		else
			var date = new Date(time);
	 
	    var year = date.getFullYear(),
	        month = date.getMonth()+1,//月份是从0开始的
	        day = date.getDate(),
	        hour = date.getHours(),
	        min = date.getMinutes(),
	        sec = date.getSeconds();
	    var preArr = Array.apply(null,Array(10)).map(function(elem, index) {
	        return '0'+index;
	    });//开个长度为10的数组 格式为 ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09"]
	 
	    var newTime = format.replace(/YY/g,year)
	        .replace(/MM/g,preArr[month]||month)
	        .replace(/DD/g,preArr[day]||day)
	        .replace(/hh/g,preArr[hour]||hour)
	        .replace(/mm/g,preArr[min]||min)
	        .replace(/ss/g,preArr[sec]||sec);
	 
	    return newTime;
	},
	//时间 -> 时间戳
	timeToTimestamp(time) {
		var date = undefined;
		if (time !== undefined)
			date = new Date(time)
		else
			date = new Date();
		
		return parseInt(date.getTime()/1000)
	},
	incYears(nYears){
		var date = new Date();
		date.setYear(date.getFullYear() + nYears);
		return date;
	},
	incDays(nDays, dt){
		var date = new Date();
		if (dt !== undefined && dt !== "")
			date = new Date(dt);
		date.setDate(date.getDate() + nDays);
		return date;
	},
	//计算孕周
	calcYunzhou(ycq, dt) {
		var curyunzhou = {week:0, day:0};
		var date1 = new Date(ycq).getTime();
		var date2 = new Date().getTime();
		if (dt !== undefined && dt !== "")
			date2 = new Date(dt).getTime();
		let date3 = 40*7*24*60*60*1000;
		let fortyago = date1 - date3;
		let yunzhou=parseInt(Math.floor(Math.abs(date2 - fortyago)/(1000*60*60*24)));
		curyunzhou.week = parseInt(yunzhou/7) ;
		curyunzhou.day = yunzhou % 7 ;
		
		/*let dates = Math.abs(date1 - date2)/(1000*60*60*24);
		curyunzhou.week = parseInt(dates/7) ;
		curyunzhou.day = dates % 7 ;*/
		
		return curyunzhou;
	},
	formatYunzhou(ycq, dt) {
		let yz = this.calcYunzhou(ycq, dt);
		if (yz === undefined || yz === null)
			return ""
		else {
			let sRes = yz.week.toString()
			if (yz.day > 0)
				sRes += "<sup>+" + yz.day.toString() + "</sup>";
				
			return sRes;
		}
		
	},
	getAge(beginStr, endStr) {
		var reg = new RegExp(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
		var beginArr = beginStr.match(reg);
		
		if (endStr===undefined || endStr==="") 
			endStr = this.formatDate(undefined, "YY-MM-DD");
		var endArr = endStr.match(reg);

		var days = 0;
		var month = 0;
		var year = 0;
		days = endArr[4] - beginArr[4];
		if (days < 0) {
			month = -1;
			days = 30 + days;
		}

		month = month + (endArr[3] - beginArr[3]);
		if (month < 0) {
			year = -1;
			month = 12 + month;
		}

		year = year + (endArr[1] - beginArr[1]);

		var yearString = year > 0 ? year + "岁" : "";
		var mnthString = month > 0 ? month + "月" : "";
		var dayString = days > 0 ? days + "天" : "";

		
		/*
		 * 1 如果岁 大于等于1 那么年龄取 几岁 2 如果 岁等于0 但是月大于1 那么如果天等于0 天小于3天 取小时
		 * 例如出生2天 就取 48小时
		 */
		var result = "";
		if (year >= 7) 
		{
			result = year;
		} 
		else 
		{
			if (year >= 1) 
			{
				result = yearString + mnthString;
			}
			else 
			{
				if (month >= 1) 
				{
					result = days > 0 ? mnthString + dayString : mnthString;
				} 
				else 
				{
					var begDate = new Date(beginArr[1], beginArr[3] - 1,
							beginArr[4], beginArr[6], beginArr[8], beginArr[10]);
					var endDate = new Date(endArr[1], endArr[3] - 1, endArr[4],
							endArr[6], endArr[8], endArr[10]);

					var between = (endDate.getTime() - begDate.getTime()) / 1000;
					days = Math.floor(between / (24 * 3600));
					var hours = Math.floor(between / 3600 - (days * 24));
					var dayString = days > 0 ? days + "天" : "";
					result = days >= 3 ? dayString : days * 24 + hours + "小时";
				}
			}
		}

		return result;
	}
	
}

export default common